﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AstekServis.Web.Data;
using System.ServiceModel.DomainServices.Client;

namespace AstekServis.Views
{
    public partial class RaporYap : ChildWindow
    {
        AsTekDomainContext context = new AsTekDomainContext();

        public RaporYap()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(RaporYap_Loaded);
            this.cmbKayitliRapor.SelectionChanged += new SelectionChangedEventHandler(cmbKayitliRapor_SelectionChanged);
        }

        void cmbKayitliRapor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbKayitliRapor.SelectedIndex > -1)
            {
                Rapor rpr = ((Rapor)cmbKayitliRapor.SelectedItem);
                txtGrafikAlanAdi.Text = rpr.ChartAlan;
                txtGrafikDegerKolon.Text = rpr.ChartDeger;
                txtGrafikSql.Text = rpr.ChartSorgu;
                txtRaporAdi.Text = rpr.Adi;
                txtRaporSql.Text = rpr.GridSorgu;
                cmbGrafikTipi.SelectedIndex = rpr.ChartTipi;
                cmbRaporTipi.SelectedIndex = rpr.Tipi;
            }
        }

        void RaporYap_Loaded(object sender, RoutedEventArgs e)
        {
            LoadRaporlar();
        }

        private void SaveRapor()
        {
            bool devam = false;
            if (cmbRaporTipi.SelectedIndex == 0)
            {
                if (!string.IsNullOrEmpty(txtRaporAdi.Text) && !string.IsNullOrEmpty(txtRaporSql.Text))
                    devam = true;
            }
            else
            {
                if (!string.IsNullOrEmpty(txtRaporAdi.Text) && !string.IsNullOrEmpty(txtRaporSql.Text) && cmbGrafikTipi.SelectedIndex > -1 && !string.IsNullOrEmpty(txtGrafikSql.Text) && !string.IsNullOrEmpty(txtGrafikDegerKolon.Text) && !string.IsNullOrEmpty(txtGrafikAlanAdi.Text))
                    devam = true;
            }
            int id = -1;
            if (cmbKayitliRapor.SelectedIndex > -1)
            {
                id = ((Rapor)cmbKayitliRapor.SelectedItem).ID;
            }


            if (devam)
            {
                LoadOperation<Rapor> operation = context.Load(context.GetRaporlarQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Rapor> op = ss as LoadOperation<Rapor>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        Rapor rpr = op.Entities.FirstOrDefault();
                        if (rpr != null)
                        {
                            rpr.Adi = txtRaporAdi.Text;
                            rpr.ChartAlan = (txtGrafikAlanAdi.Text == string.Empty) ? "0" : txtGrafikAlanAdi.Text;
                            rpr.ChartDeger = (txtGrafikDegerKolon.Text == string.Empty) ? "0" : txtGrafikDegerKolon.Text;
                            rpr.ChartSorgu = (txtGrafikSql.Text == string.Empty) ? "0" : txtGrafikSql.Text;
                            rpr.ChartTipi = (cmbGrafikTipi.SelectedIndex == -1) ? byte.MinValue : (byte)cmbGrafikTipi.SelectedIndex;
                            rpr.GridSorgu = txtRaporSql.Text;
                            rpr.Tipi = (byte)cmbRaporTipi.SelectedIndex;

                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            }, null);
                        }
                        else
                        {
                            Rapor newrpr = new Rapor();
                            newrpr.Adi = txtRaporAdi.Text;
                            newrpr.ChartAlan = (txtGrafikAlanAdi.Text == string.Empty) ? "0" : txtGrafikAlanAdi.Text;
                            newrpr.ChartDeger = (txtGrafikDegerKolon.Text == string.Empty) ? "0" : txtGrafikDegerKolon.Text;
                            newrpr.ChartSorgu = (txtGrafikSql.Text == string.Empty) ? "0" : txtGrafikSql.Text;
                            newrpr.ChartTipi = (cmbGrafikTipi.SelectedIndex == -1) ? byte.MinValue : (byte)cmbGrafikTipi.SelectedIndex;
                            newrpr.GridSorgu = txtRaporSql.Text;
                            newrpr.Tipi = (byte)cmbRaporTipi.SelectedIndex;

                            context.Rapors.Add(newrpr);
                            context.SubmitChanges(so =>
                            {
                                if (so.HasError)
                                    MessageBox.Show("Hata:\r\n" + so.Error.Message);
                                else
                                {
                                    LoadRaporlar();
                                }
                            }, null);
                        }
                    }
                };
            }
        }

        private void LoadRaporlar()
        {
            EntityQuery<Rapor> qry = context.GetRaporlarQuery();
            LoadOperation<Rapor> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<Rapor> op = ss as LoadOperation<Rapor>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    cmbKayitliRapor.ItemsSource = op.Entities;
                    cmbKayitliRapor.DisplayMemberPath = "Adi";
                }
            };
        }

        private void DeleteRapor()
        {
            if (cmbKayitliRapor.SelectedIndex > -1)
            {
                int id = ((Rapor)cmbKayitliRapor.SelectedItem).ID;
                LoadOperation<Rapor> operation = context.Load(context.GetRaporlarQuery().Where(x => x.ID == id));
                operation.Completed += (ss, oo) =>
                {
                    LoadOperation<Rapor> op = ss as LoadOperation<Rapor>;
                    if (op.HasError)
                    {
                        System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                        System.Windows.MessageBoxButton.OK);
                        op.MarkErrorAsHandled();
                    }
                    else
                    {
                        Rapor usr = op.Entities.FirstOrDefault();
                        context.Rapors.Remove(usr);
                        context.SubmitChanges(so =>
                        {
                            if (so.HasError)
                                MessageBox.Show("Hata:\r\n" + so.Error.Message);
                            else
                            {
                                LoadRaporlar();
                            }
                        }, null);
                    }
                };
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SaveRapor();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteRapor();
        }
    }
}

